<?php
namespace common\models;




class TradeModel extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'trade_model';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['mch_id', 'priority', 'next_action', 'compute_cycle'], 'integer'],
            [['model_condition'], 'string'],
            [['model_code', 'trigger_type'], 'string', 'max' => 32],
            [['model_name'], 'string', 'max' => 16],
            [['model_desc'], 'string', 'max' => 128],
            [['assign_type'], 'string', 'max' => 64],
            [['state'], 'string', 'max' => 1],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'model_id' => 'Model ID',
            'mch_id' => '商户id',
            'model_code' => '模式编码',
            'model_name' => '模式名称',
            'model_desc' => '模式描述',
            'model_condition' => '模式条件',
            'trigger_type' => '触发类型1投资报单触发 2订单触发',
            'assign_type' => '账本分配方式',
            'priority' => '触发优先级 越小越早触发',
            'next_action' => '下一步触发动作 对应model_id ',
            'compute_cycle' => '1实时执行 2定时执行',
            'state' => '1正常 0禁用',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();
		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		foreach($extends as $extend)
		{

		}

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

## 根据触发事件获取配置
    public static function getTriggerEvent($triggerType)
    {
        $data = self::find()->where(['mch_id'=>MCHID,'state'=>1])
            ->andWhere(['like','trigger_type',$triggerType.','])
            ->orderBy('priority asc')
            ->asarray()->all();
        return $data;
    }


    ## 查询分享模式配置信息
    public static function getRecommendConf($code)
    {
        $where = ['a.model_code'=>$code,'a.mch_id'=>MCHID];
        $data  = self::find()->select(['a.*','b.*'])->alias('a')
            ->leftJoin('trade_recommend as b','a.model_id = b.model_id')
            ->where($where)
            ->asarray()->one();
        return $data;

    }

    ## 会员升级模式配置
    public static function getUpgradeConf($code)
    {
        $where = ['model_code'=>$code,'mch_id'=>MCHID];
        $data  = TradeUpgrade::find()->select(['a.*','b.*'])->alias('a')
            ->leftJoin('trade_model as b','a.model_id = b.model_id')
            ->where($where)
            ->orderBy('grade asc')
            ->asarray()->all();
        return $data;
    }

    ## 获取静态释放配置
    public static function getStaticReleaseConf($code)
    {
        $where = ['a.model_code'=>$code,'a.mch_id'=>MCHID];
        $data  = self::find()->select(['a.*','b.*'])->alias('a')
            ->leftJoin('trade_static_release as b','a.model_id = b.model_id')
            ->where($where)
            ->asarray()->one();
        return $data;
    }


    ## 获取加速释放配置
    public static function getSpeedBonusConf($code,$model_id)
    {
        $where = ['model_code'=>$code,'b.model_id'=>$model_id];

        $data  = TradeSpeedBonus::find()->select(['a.*','b.*'])->alias('a')
            ->leftJoin('trade_model as b','a.model_id = b.model_id')
            ->where($where)
            ->orderBy('level asc')
            ->asarray()->all();
        return $data;
    }

    ## 获取团队奖配置
    public static function getTeamBonusConf($code)
    {
        $where = ['model_code'=>$code,'mch_id'=>MCHID];
        $data  = TradeTeamBonus::find()->select(['a.*','b.*'])->alias('a')
            ->leftJoin('trade_model as b','a.model_id = b.model_id')
            ->where($where)
            ->orderBy('grade asc')
            ->asarray()->all();
        return $data;
    }
}